package com.sdry.web.controller.zc.quality;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.llm.User;
import com.sdry.model.zc.AbnormalDisposal;
import com.sdry.model.zc.ZcPage;
import com.sdry.service.zc.AbnormalDisposalService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: AbnormalDisposalController 
 * @Description:生产异常处置单Controller
 * @Author zc
 * @Date 2018年11月2日 下午4:12:04 
 * @version 1.0
 */
@RequestMapping(value="/abnormalDisposal")
@Controller
public class AbnormalDisposalController {

	@Resource
	private AbnormalDisposalService abnormalDisposalService;
	
	/**
	 * 分页查询
	 * @param abnormalDisposal 查询条件
	 * @param page 分页条件
	 * @param response
	 */
	@RequestMapping(value="/listPageAbnormalDisposal")
	public void listPageAbnormalDisposal(AbnormalDisposal abnormalDisposal,ZcPage page,HttpServletResponse response){
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (abnormalDisposal.getAdNum() != null) {
			map.put("adNum", abnormalDisposal.getAdNum());
		} else {
			map.put("adNum", "");
			abnormalDisposal.setAdNum("");
		}
		if (abnormalDisposal.getReserve1() != null) {
			map.put("reserve1", abnormalDisposal.getReserve1());
		} else {
			map.put("reserve1", "");
			abnormalDisposal.setReserve1("");
		}
		int count = abnormalDisposalService.countAbnormalDisposal(abnormalDisposal);
		List<AbnormalDisposal> abnormalDisposalList = abnormalDisposalService.listPageAbnormalDisposal(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(abnormalDisposalList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 保存生产异常处置单
	 * @param abnormalDisposal
	 * @param response
	 */
	@RequestMapping(value="/saveAbnormalDisposal")
	public void saveAbnormalDisposal(AbnormalDisposal abnormalDisposal,HttpServletResponse response,HttpSession session){
		int isSuccess = 0;
		User user = (User) session.getAttribute("currentUser");
		if (abnormalDisposal.getAdID() == null || "".equals(abnormalDisposal.getAdID())) {
			abnormalDisposal.setSquadLeader(user.getUserName());
			isSuccess = abnormalDisposalService.insertAbnormalDisposal(abnormalDisposal);
		} else if("edit".equals(abnormalDisposal.getReserve5())){
			isSuccess = abnormalDisposalService.editAbnormalDisposal(abnormalDisposal);
		}else {
			//基地经理签字
			abnormalDisposal.setManager(user.getUserName());
			isSuccess = abnormalDisposalService.updateAbnormalDisposal(abnormalDisposal);
		}
		try {
			ResponseUtil.write(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 发起申请
	 * @param abnormalDisposal
	 * @param response
	 */
	@RequestMapping("approvalAbnormalDisposal")
	public void approvalAbnormalDisposal(AbnormalDisposal abnormalDisposal,HttpServletResponse response,HttpSession session){
		int affact = abnormalDisposalService.approvalAbnormalDisposal(abnormalDisposal);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除
	 * @param abnormalDisposal
	 * @param response
	 * @param session
	 */
	@RequestMapping("deleteAbnormalDisposal")
	public void deleteAbnormalDisposal(AbnormalDisposal abnormalDisposal,HttpServletResponse response,HttpSession session){
		int affact = abnormalDisposalService.deleteAbnormalDisposal(abnormalDisposal);
		try {
			ResponseUtil.write(response, affact);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
